package com.bilibili.dubbo.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bilibili.dubbo.api.db.BlackListApi;
import com.bilibili.dubbo.api.mapper.BlackListMapper;
import com.bilibili.model.db.BlackList;
import com.bilibili.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author HuangRongFu
 * 2021/11/18 星期四8:09 下午
 */

@DubboService
public class BlackListApiImpl implements BlackListApi {

    @Autowired
    private BlackListMapper blackListMapper;

    /**
     * 黑名单列表
     *
     * @param: page
     * @param: pageSize
     * @Return: PageResult<BlackList>
     */
    @Override
    public PageResult<BlackList> findPage(Long userId, Long page, Long pageSize) {

        //设置分页参数
        Page<BlackList> blackList = new Page<>(page, pageSize);

        //构建查询条件
        QueryWrapper<BlackList> qw = new QueryWrapper<>();
        qw.eq("user_id", userId);

        //分页查询
        Page<BlackList> selectPage = blackListMapper.selectPage(blackList, qw);

        //构建PageResult
        return new PageResult<BlackList>(page,pageSize,selectPage.getTotal(),selectPage.getRecords());
    }

    /**
     * 移除黑名单
     *
     * @param loginUserId
     * @param blackUserId
     */
    @Override
    public void remove(Long loginUserId, Long blackUserId) {

        QueryWrapper<BlackList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", loginUserId)
                .eq("black_user_id",blackUserId);
        blackListMapper.delete(queryWrapper);
    }
}
